# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper

  def vote_for(votable)
    render :partial => 'widgets/voter', :locals => {:votable => votable}
  end

  def commit_vote(thought_id, value)
    if value == 0
      page.replace "vote_on_t#{thought_id}", :partial => 'widgets/voter', 
      :locals => {:votable => Thought.find(thought_id)}
    else 
      page.replace "vote_on_t#{thought_id}", :partial => 'widgets/voter',
      :locals => {:votable => Thought.find(thought_id), :vote => value }
    end
    page.visual_effect :highlight, "vote_count_for_t#{thought_id}"
  end

#source: http://snippets.dzone.com/tag/timestamp
def time_ago_or_time_stamp(from_time, to_time = Time.now, include_seconds = true, detail = false)
  from_time = from_time.to_time if from_time.respond_to?(:to_time)
  to_time = to_time.to_time if to_time.respond_to?(:to_time)
  distance_in_minutes = (((to_time - from_time).abs)/60).round
  distance_in_seconds = ((to_time - from_time).abs).round
  case distance_in_minutes
  when 0..1           then time = (distance_in_seconds < 60) ? "#{distance_in_seconds} seconds ago" : '1 minute ago'
  when 2..59          then time = "#{distance_in_minutes} minutes ago"
    when 60..90         then time = "1 hour ago"
  when 90..1440       then time = "#{(distance_in_minutes.to_f / 60.0).round} hours ago"
    when 1440..2160     then time = '1 day ago' # 1-1.5 days
  when 2160..2880     then time = "#{(distance_in_minutes.to_f / 1440.0).round} days ago" # 1.5-2 days
    else time = from_time.strftime("%a, %d %b %Y")
  end
  return time_stamp(from_time) if (detail && distance_in_minutes > 2880)
  return time
end

end
